package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.UserBank;
import com.hyk.dto.R;
import com.hyk.dto.UserBankDto;
import com.hyk.feign.UserBankFeign;
import com.hyk.service.UserBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户银行控制器")
@RequestMapping("/userBanks")
public class UserBankController implements UserBankFeign {

    @Resource
    private UserBankService userBankService;

    @GetMapping
    @ApiOperation("银行卡列表")
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> list(@ApiIgnore Page<UserBank> page,
                                  String usrId){
        QueryWrapper<UserBank> wrapper = new QueryWrapper<>();
        if (usrId != null && usrId != ""){
            wrapper.eq("user_id",usrId);
        }
        wrapper.orderByDesc("created");
        return R.ok(userBankService.page(page,wrapper));
    }

    @PatchMapping
    @ApiOperation("编辑银行卡信息")
    //@PreAuthorize("hasAuthority('user_bank_update')")
    public R update(@RequestBody UserBank userBank){
        boolean update = userBankService.updateById(userBank);
        if (update) {
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/status")
    @ApiOperation("修改状态")
    //@PreAuthorize("hasAuthority('user_bank_update')")
    public R changeStatus(String id,String status){
        UserBank userBank = new UserBank();
        userBank.setId(Long.parseLong(id));
        userBank.setStatus(Byte.parseByte(status));
        boolean update = userBankService.updateById(userBank);
        if (update) {
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("/current")
    @ApiOperation("用户银行卡信息")
    public R<UserBank> current(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        QueryWrapper<UserBank> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<UserBank> list = userBankService.list(wrapper);
        UserBank userBank = CollectionUtils.isEmpty(list) ? null:list.get(0);
        return R.ok(userBank);
    }

    @PostMapping("/bind")
    @ApiOperation("添加银行卡")
    public R addUserBank(@RequestBody UserBank userBank){
        userBankService.addUserBank(userBank);
        return R.ok();
    }

    @Override
    @GetMapping("/getUserBank")
    public Map<Long,UserBankDto> getUserBank(List<Long> ids) {
        QueryWrapper<UserBank> wrapper = new QueryWrapper<>();
        wrapper.in("user_id",ids);
        List<UserBank> list = userBankService.list(wrapper);
        Map<Long,UserBankDto> map = new HashMap<>();
        for (UserBank userBank: list) {
            UserBankDto userBankDto = new UserBankDto();
            BeanUtils.copyProperties(userBank,userBankDto);
            map.put(userBankDto.getUserId(),userBankDto);
        }
        return map;
    }
}
